<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,user-scalable=no,minimal-ui">
    <title>菩提阁</title>
    <link rel="icon" href="./../images/favico.ico">
    <!--不同屏幕尺寸根字体设置-->
    <script src="./../js/base.js"></script>
    <!--element-ui的样式-->
    <link rel="stylesheet" href="../../backend/plugins/element-ui/index.css" />
    <!--引入vant样式-->
    <link rel="stylesheet" href="../styles/vant.min.css"/>
    <!-- 引入样式  -->
    <link rel="stylesheet" href="../styles/index.css" />
    <!--本页面内容的样式-->
    <link rel="stylesheet" href="./../styles/login.css" />
</head>
<body>
<div id="login" v-loading="loading">
    <div class="divHead">登录</div>

    <div class="divContainer">
        <el-input placeholder=" 请输入电子邮箱" v-model="form.email" maxlength='50'/>
        </el-input>
        <div class="divSplit"></div>
        <el-input placeholder=" 请输入验证码" v-model="form.code" maxlength='20'/>
        </el-input>

        <span @click='getCode' v-show="show">获取验证码</span>
        <!--用户点击完【获取验证码】后倒计时60s-->
        <span v-show="!show" class="count">重新获取（{{count}} s）</span>


    </div>
    <div class="divMsg" v-if="msgFlag">邮箱输入不正确，请重新输入</div>
    <el-button type="primary"
               :class="{btnSubmit:1===1,btnNoPhone:!form.code||!form.email,btnPhone:form.code&&form.email}"
               @click="btnLogin">
        登录
    </el-button>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../backend/plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../backend/plugins/element-ui/index.js"></script>
<!-- 引入vant样式 -->
<script src="./../js/vant.min.js"></script>
<!-- 引入axios -->
<script src="../../backend/plugins/axios/axios.min.js"></script>
<script src="./../js/request.js"></script>
<script src="./../api/login.js"></script>
</body>
<script>
    new Vue({
        el: "#login",
        data() {
            return {
                show: true, // 控制【获取验证码】以及倒计时的显示开关
                count: '',  // 倒计时时长
                timer: null,    // 计时器
                form: {
                    email: '',
                    code: ''
                },
                msgFlag: false,
                loading: false
            }
        },
        computed: {},
        created() {
        },
        mounted() {
        },
        methods: {
            getCode() {
                const TIME_COUNT = 60;  // 验证码倒计时时长60s
                this.form.code = ''
                // const regex = /^(13[0-9]{9})|(15[0-9]{9})|(17[0-9]{9})|(18[0-9]{9})|(19[0-9]{9})$/;  // 手机号正则表达式
                const regex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; // 邮箱正则表达式
                if (regex.test(this.form.email)) {
                    this.msgFlag = false
                    // this.form.code = (Math.random() * 1000000).toFixed(0)
                    const res = sendMsgApi({email: this.form.email})    // 向后端发送用户的邮箱，让后端给用户发送验证码邮件
                    this.$notify({type: 'success', message: '验证码发送成功'})

                    if (!this.timer) {
                        this.count = TIME_COUNT;    // 设置倒计时时长
                        this.show = false;  // 不显示【获取验证码】，显示倒计时
                        this.timer = setInterval(() => {
                            if (this.count > 0 && this.count <= TIME_COUNT) {
                                this.count--;
                            } else {    // 倒计完了
                                this.show = true;   // 不显示倒计时，显示【获取验证码】
                                clearInterval(this.timer);
                                this.timer = null;
                            }
                        }, 1000)
                    }
                } else {
                    this.msgFlag = true
                }
            },
            async btnLogin() {
                if (this.form.email && this.form.code) {
                    this.loading = true
                    const res = await loginApi(this.form)
                    this.loading = false
                    if (res.code === 1) {
                        localStorage.setItem('userInfo', JSON.stringify(res.data)) // 存储后端返回的用户对象
                        sessionStorage.setItem("userEmail", this.form.email)
                        window.requestAnimationFrame(() => {
                            this.$notify({type: 'success', message: '登录成功'})
                            window.location.href = '/front/index.html'
                        })
                    } else {
                        this.$notify({type: 'warning', message: res.msg});
                    }
                }
                if (!this.form.email) {
                    this.$notify({type: 'warning', message: '请输入电子邮箱'});
                } else if (!this.form.code) {
                    this.$notify({type: 'warning', message: '请输入验证码'});
                }
            }
        }
    })
</script>
</html>